package com.isczy.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import com.isczy.vod.service.VodService;
import com.isczy.vod.utils.InitVodClient;
import com.isczy.vod.utils.YamlConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
public class VodController {

    @Autowired
    private VodService vodService;

    /***
     * ***************************************
     * 功能描述:上传视频到阿里云
     * @param file
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-07 15:29】
     * @author modify:
     */
    @PostMapping("uploadVideoAly")
    public DataResult uploadVideoAly(MultipartFile file) {
        try {
            //返回上传视频id
            return DataResult.builder()
                    .data(vodService.uploadVideoAly(file))
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("上传视频到阿里云失败")
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据视频id删除阿里云视频
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-08 11:54】
     * @author modify:
     */
    @DeleteMapping("removeAlyVideo/{id}")
    public DataResult removeAlyVideo(@PathVariable String id) {
        try {
            vodService.removeAlyVideoById(id);
            return DataResult.builder().successBuild();
        }catch(Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("删除阿里云视频失败")
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:删除多个阿里云视频的方法
     * @param videoIdList 参数多个视频id  List videoIdList
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-08 11:55】
     * @author modify:
     */
    @DeleteMapping("delete-batch")
    public DataResult deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        try {
            vodService.removeMoreAlyVideo(videoIdList);
            return DataResult.builder().successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("删除多个阿里云视频失败")
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据视频id获取视频凭证
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 14:40】
     * @author modify:
     */
    @GetMapping("getPlayAuth/{id}")
    public DataResult getPlayAuth(@PathVariable String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client =
                    InitVodClient.initVodClient(YamlConstant.ACCESS_KEY_ID, YamlConstant.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return DataResult.builder().data("playAuth",playAuth).successBuild();
        }catch(Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据视频id获取视频凭证失败")
                    .build();
        }
    }
}
